﻿singleApp.controller('machineAppointCtrl', ['$http', '$scope', 'PageService', 'AppDataService', function ($http, $scope, PageService, AppDataService) {
    $scope.list = [];
    $scope.Page = {
        pageindex: 1,
        pagesize: "10",
        name: "",
        TotalPages: 0,
        TotalRecords: 0,
        PageWindowSize: 10,
        PageRange: [],
    };
    $scope.selectAll = false;

    //Init
    $scope.init = function () {
        layer.load(2);

        var parms = [];
        parms.push("pageindex=" + $scope.Page.pageindex);
        parms.push("pagesize=" + $scope.Page.pagesize);
        parms.push("name=" + $scope.Page.name);
        var api_url = window.singleConfig.rootUrl + "api/admin/MachineAppointService/pageList?" + parms.join("&");

        $http.get(api_url).then(function (result) {
            var data = result.data;
            $scope.list = data.records;
            $scope.Page.TotalPages = data.totalPages;
            $scope.Page.TotalRecords = data.count;
            $scope.Page.PageRange = PageService.getPageRange($scope.Page.pageindex, $scope.Page.PageWindowSize, $scope.Page.TotalPages);

            angular.forEach($scope.list, function (data, index, array) {
                data.selected = false;
            });

            //console.log($scope.list);
            layer.closeAll();
        }, function () { });
    };

    $scope.init();


    $scope.GoToPage = function (page) {
        if (page == $scope.Page.pageindex)
            return;
        $scope.Page.pageindex = page;
        $scope.init();
    };

    $scope.search = function () {
        $scope.init();
    };

    $scope.selectChange = function (item) {
        item.selected = item.selected == true ? false : true;

        var temp = 0;
        angular.forEach($scope.list, function (data, index, array) {
            if (data.selected == true) {
                temp += 1;
            }
        });

        if (temp == 0) {
            $scope.selectAll = false;
        }
        if (temp == $scope.list.length) {
            $scope.selectAll = true;
        }
    };

    $scope.selectAllChange = function (item) {
        $scope.selectAll = $scope.selectAll == true ? false : true;

        angular.forEach($scope.list, function (data, index, array) {
            data.selected = $scope.selectAll;
        });
    };

    $scope.operate = function (value) {
        var ids = [];
        angular.forEach($scope.list, function (data, index, array) {
            if (data.selected == true) {
                ids.push(data.id);
            }
        });

        if (ids.length == 0) {
            layer.msg("请至少选择一项！");
            return;
        }

        layer.confirm("确定提交吗？", {
            btn: ['确认', '取消']
        }, function () {
            layer.load(2);

            var url = window.singleConfig.rootUrl + "/api/admin/MachineAppointService/operate?ids=" + ids.join(",") + "&value=" + value;
            $http.get(url).then(function (result) {
                layer.msg("提交成功！");
                setTimeout(function () {
                    layer.closeAll();
                    $scope.init();
                }, 500);
            }, function () { });
        }, function () {

        });
    };

    $scope.delete = function (item) {
        layer.confirm("确认删除该预约吗？", {
            btn: ['确认', '取消']
        }, function () {
            layer.load(2);

            var url = window.singleConfig.rootUrl + "/api/admin/MachineAppointService/delete?id=" + item.id;
            $http.get(url).then(function (result) {
                layer.msg("删除成功！");
                setTimeout(function () {
                    layer.closeAll();
                    $scope.init();
                },500);
            }, function () { });
        }, function () {

        });
    };
}]);